package bobaini.com.easy;

import com.rabbitmq.client.*;

/**
 * @author: liuyi
 * ClassName: Consumer
 * Version: 1.0.0
 * DateTime: 2021/03/31-10:36:00
 * Description:
 */
public class Consumer {
    private final static String QUEUE_NAME = "hello";

    private final static Integer taskSize = 10;

    public static void main(String[] args) throws Exception {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("localhost");
        factory.setPort(5672);
        factory.setUsername("guest");
        factory.setPassword("guest");
        Connection connection = factory.newConnection();
        /**
         * channel 实现了自动 close 接口 自动关闭 不需要显示关闭
         */
        Channel channel = connection.createChannel();
        System.out.println(" 等待接收消息 ......... ");
        /**
         * 推送的消息如何进行消费的接口回调
         */
        DeliverCallback deliverCallback = (consumerTag, delivery) -> {
            String message = new String(delivery.getBody());
            System.out.println(message);
        };
        /**
         * 取消消费的一个回调接口 如在消费的时候队列被删除掉了
         */
        CancelCallback cancelCallback = (consumerTag) -> {
            System.out.println(" 消息消费被中断");
        };
        /**
         * 消费者消费消息
         * 1. 消费哪个队列
         * 2. 消费成功之后是否要自动应答 true 代表自动应答 false 手动应答
         * 3. 消费者未成功消费的回调
         */
        channel.basicConsume(QUEUE_NAME, true, deliverCallback, cancelCallback);
    }
}
